package org.jeecg.modules.activiti.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.activiti.entity.FormDesign;
import org.jeecg.modules.activiti.mapper.FormDesignMapper;
import org.jeecg.modules.activiti.service.IFormDesignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 自定义表单
 * @author: 黄劲博
 * @date: 2021/6/25 14:20
 */
@RestController
@RequestMapping("/formDesign")
@Api(tags = "自定义表单")
public class FormDesignController {

    @Autowired
    private FormDesignMapper formDesignMapper;
    @Autowired
    private IFormDesignService formDesignService;

    @GetMapping("/page")
    @ApiOperation("工作流模型分页")
    @AutoLog
    public Object page(FormDesign formDesign, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) {
        Result<Page<FormDesign>> pageResult = new Result<>();
        Page<FormDesign> page = new Page<>(pageNo, pageSize);
        QueryWrapper<FormDesign> queryWrapper = QueryGenerator.initQueryWrapper(formDesign, req.getParameterMap());
        pageResult.setResult(formDesignService.page(page, queryWrapper));
        return pageResult;
    }

    @GetMapping("/list")
    @ApiOperation("工作流模型分页")
    @AutoLog
    public Object list() {
        QueryWrapper<FormDesign> formDesignQueryWrapper = new QueryWrapper<>();
        formDesignQueryWrapper.eq("status", "1");
        return Result.ok(formDesignService.list(formDesignQueryWrapper));
    }

    @GetMapping("/info/{id}")
    @ApiOperation("工作流模型分页")
    @AutoLog
    public Object getFormDesign(@PathVariable String id) {
        return Result.ok(formDesignService.getById(id));
    }

    @GetMapping("/dynamicData")
    @ApiOperation("自定义表单动态数据")
    @AutoLog
    public Object dynamicData() {
        return Result.ok(formDesignService.getDynamicData());
    }

    @GetMapping("/formNub/{formNub}")
    @ApiOperation("工作流模型分页")
    @AutoLog
    public Object getFormDesignByFormNub(@PathVariable String formNub) {
        QueryWrapper<FormDesign> formDesignQueryWrapper = new QueryWrapper<FormDesign>();
        formDesignQueryWrapper.eq("status", "1");
        formDesignQueryWrapper.eq("from_number", formNub);
        return Result.ok(formDesignService.getOne(formDesignQueryWrapper));
    }

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("创建工作流模型")
    @AutoLog
    public Object createModel(@RequestBody FormDesign formDesign) {
        if (formDesignService.insert(formDesign)) {
            return Result.ok("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }

    @PutMapping("/update")
    @ApiOperation("更新工作流模型")
    @AutoLog
    public Object updateModel(@RequestBody FormDesign formDesign) {
        if (formDesignService.update(formDesign)) {
            return Result.ok("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除工作模型")
    @AutoLog
    public Object delete(String id) {
        if (formDesignService.removeById(id)) {
            return Result.ok("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }

    @DeleteMapping("/deleteBatch")
    @ApiOperation("批量删除工作模型")
    @AutoLog
    public Object deleteBatch(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        if (formDesignService.removeByIds(idList)) {
            return Result.ok("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }

    /*@GetMapping("/info/{id}")
    @ApiOperation("表单详情")
    @AutoLog
    public Object loadProcessJson(@PathVariable String id) {
        return Result.ok(formDesignService.getById(id));
    }
*/
}
